//
//  GDAdVertiseView.m
//  Gunterapple
//
//  Created by 热带雨林 on 2021/1/16.
//  Copyright © 2021 热带雨林. All rights reserved.
//

#import "GDAdVertiseView.h"
#import "UIImageView+WebCache.h"
@interface GDAdVertiseView ()
@property (nonatomic,strong) UIImageView *adImageView;
@property (nonatomic, strong) UILabel *timerLabel;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSInteger adTimer;
@property (nonatomic, strong) UILabel *hintLabel;//提示  广告|已预加载
@end

@implementation GDAdVertiseView

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor redColor];
        self.adTimer = 5; //定时的5秒
        [self setUpUI];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushADvc)];
        [self addGestureRecognizer:tap];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timercount) userInfo:nil repeats:YES];
    }
    return self;
}
- (void)setUpUI{
    self.adImageView = [[UIImageView alloc]initWithFrame:GDScreenBounds];
    [self addSubview:self.adImageView];
    [self addSubview:self.timerLabel];
    [self addSubview:self.hintLabel];
}
- (void)setImageUrl:(NSString *)imageUrl{
    _imageUrl = imageUrl;
    GDWeakSelf
    //加载图片
    [self.adImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImage imageNamed:@"newbackImage"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        weakSelf.adImageView.image = image;
    }];
}
#pragma mark  - 跳转到广告页控制器
- (void)pushADvc{
    [self dissmiss];
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushtoad" object:nil];
}
#pragma mark - 显示视图
- (void)show{
//       适配iOS13 与 iOS12
    UIWindow *window = nil;
    if([[[UIApplication sharedApplication] delegate] window]){
        window = [[[UIApplication sharedApplication] delegate] window];
    }else{
        
        if(@available(iOS 13.0,*)){
            NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
            //强转
            UIWindowScene *windowScene = (UIWindowScene *)array[0];
            //如果是普通的APP，可以使用
//                  SceneDelegate * delegate = (SceneDelegate *)windowScene.delegate;
//                  UIWindow * mainWindow = delegate.window;
            //由于在sdk开发中，引入不了scenedelegate的头文件，所以需要使用KVC获取宿主app的window
            UIWindow *mainWindow = [windowScene valueForKeyPath:@"delegate.window"];
            if(mainWindow){
                window = mainWindow;
            }else{
                window = [UIApplication sharedApplication].windows.lastObject;
            }
        }else{
            window = [UIApplication sharedApplication].keyWindow;
        }
    }
    [window addSubview:self];
    /**
     将一个UIView显示在最前面只需要调用其父视图的 bringSubviewToFront（）方法。

     将一个UIView层推送到背后只需要调用其父视图的 sendSubviewToBack（）方法。

     如：

     [A bringSubviewToFront:B]; B视图在A视图上面

     [A sendSubviewToBack:B]; B视图在A视图下面
     */
    [window bringSubviewToFront:self];
}
#pragma mark - timer
- (void)timercount{
    self.adTimer --;
//    self.timerLabel.text = [NSString stringWithFormat:@"跳过%ld",(long)self.adTimer];
    self.timerLabel.text = @"跳过";
    if(self.adTimer <= 0){
        [self dissmiss];
    }
}

#pragma mark -lazy
- (UILabel *)timerLabel{
    if (!_timerLabel) {
        _timerLabel = [[UILabel alloc]initWithFrame:CGRectMake(GDScreenW - 120, GDScreenH - 90, 70, 27)];
        _timerLabel.backgroundColor = [UIColor colorWithRed:125/256.0 green:125/256.0  blue:125/256.0  alpha:0.5];
        _timerLabel.textColor = [UIColor whiteColor];
        _timerLabel.textAlignment = NSTextAlignmentCenter;
        _timerLabel.layer.masksToBounds = YES;
        _timerLabel.layer.cornerRadius = 10.0f;
        [_timerLabel setUserInteractionEnabled:YES];
        //添加手势
        UITapGestureRecognizer *timerLabelTap =  [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dissmiss)];
        [_timerLabel addGestureRecognizer:timerLabelTap];
    }
    return _timerLabel;
}
- (UILabel *)hintLabel{
    if(!_hintLabel){
        _hintLabel = [[UILabel alloc]initWithFrame:CGRectMake(GDScreenW - 100, 50, 110, 27)];
        _hintLabel.text = @"广告|已预加载";
        _hintLabel.font = [UIFont systemFontOfSize:12.0f];
        _hintLabel.textColor = [UIColor whiteColor];
    }
    return _hintLabel;
}
#pragma mark - diss
- (void)dissmiss{
    [UIView animateWithDuration:0.1f animations:^{
        self.alpha = 0.0f;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    [self.timer invalidate];
    self.timer = nil;
}
@end
